package com.woniu.houtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.houtai.config.Converter;
import com.woniu.houtai.entity.Business;
import com.woniu.houtai.entity.Product;
import com.woniu.houtai.entity.Shop;
import com.woniu.houtai.entity.Type;
import com.woniu.houtai.service.BusinessService;
import com.woniu.houtai.service.ProductService;
import com.woniu.houtai.service.ShopService;
import com.woniu.houtai.utils.CookieUtils;
import com.woniu.houtai.utils.FileUtils;
import com.woniu.houtai.utils.JwtUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@RequestMapping("/product")
@Controller
public class ProductController {
    private static final String pathName = "http://192.168.201.161:10002/imgs/";
    @Autowired
    ProductService productService;

    @Autowired
    BusinessService businessService;

    @Autowired
    ShopService shopService;




    @RequestMapping("/insertProduct")
    @ResponseBody
    public String insert(Product product, MultipartFile imageUrl) throws IOException {
        String ip = "192.168.201.161"; //目标ip
        String filePath = "//"+ip+"/imgs/";
        if (product.getShopId() != null) {
            // 图片保存
            if (imageUrl.isEmpty()) {
                return "fail";
            } else {
                String newFilename = FileUtils.copy(imageUrl, filePath);
                //写入数据库
                //创建数据库中的图片地址
                String sqlFileName = pathName + newFilename;
                product.setImgSrc(sqlFileName);
                Integer result = productService.insertProductInfo(product);
                System.out.println("result: " + result);
                return "ok";
            }
        } else {
            return "产品上架失败";
        }
    }

    @RequestMapping("/updateProduct")
    @ResponseBody
    public String update(Product product, MultipartFile imageUrl) throws IOException {
        System.out.println(product);
        String ip = "192.168.201.161"; //目标ip
        String filePath = "//"+ip+"/imgs/";
        String sqlFileName = "";
        if (product != null) {
            // 图片保存
            if (imageUrl!=null){
                String newFilename = FileUtils.copy(imageUrl, filePath);
                sqlFileName = pathName + newFilename;
                product.setImgSrc(sqlFileName);
            }
            //写入数据库
            //创建数据库中的图片地址
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",product.getId());
            boolean update = productService.update(product, queryWrapper);
            System.out.println("update: " + update);
            return "ok";

        } else {
            return "产品修改失败";
        }
    }

    private Business getCurrentBusiness(HttpServletRequest request) {
        //获取已登录商家信息
        // 1.先从request中拿到token
        // 2.从token中获取用户名
        // 3.根据用户名查询到用户信息
        String userTokenFromCookie = CookieUtils.getUserTokenFromCookie(request);
        String account = JwtUtils.getAccountWithoutException(userTokenFromCookie);
        return businessService.getBusinessByName(account);
    }

    @RequestMapping("productList")
    @ResponseBody
    public Page<Product> selectProductByShopId(Integer currentPage,Integer pageSize,Integer shopId){
        Page<Product> productPage = productService.selectProduct(currentPage, pageSize, shopId);
        return productPage;
    }

    @RequestMapping("selectProductById")
    public String selectShopByShopId(Integer productId, Model model){
        Product product = productService.selectProductByPid(productId);
        model.addAttribute("product",product);
        return "product";
    }

}

